<?php
defined('ABSPATH') or die("No script kiddies please!");
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-formats', array( 'gallery', 'video' ) );

add_filter('show_admin_bar', '__return_false');

// register menu
register_nav_menu('category-menu',__( 'Category Menu' ));

//custom image size
add_image_size( 'featured-lrg', 473, 253 );
add_image_size( 'featured-thumb', 492, 282 );
add_image_size( 'featured-thumb-small', 129, 72 );
add_image_size( 'review-thumb', 160, 92 );
add_image_size( 'article-thumb', 125, 70 );
add_image_size( 'detail_viewmore_thumb', 140, 80 );
add_image_size( 'cate_thumb', 180, 100 );

//get default image
function get_default_image($width = 492, $height = 275){
    if ( has_post_thumbnail() ){
        the_post_thumbnail('featured-thumb');
    }
    else 
    {
        echo  "/pncm/wp-content/themes/pncm/images/img_default.jpg";
    }
}
//get background image
function get_bg_image($postID)
{
    if ( has_post_thumbnail() ){
		$arr = wp_get_attachment_image_src( get_post_thumbnail_id( $postID ), 'single-post-thumbnail' );
        $image_link = $arr[0]; 
		
		//$image_link = wp_get_attachment_url( get_post_thumbnail_id($postID, 'thumbnail') ); //wp_get_attachment_url(get_post_thumbnail_id($postID));
		//$image_link = "/wp-content/themes/pncm/images/img_default.jpg";
    }
    else 
    {
        $image_link = "/wp-content/themes/pncm/images/img_default.jpg";
    }
    return $image_link;
}
//get Vietnamese date time
function sw_get_current_weekday() {
    date_default_timezone_set('Asia/Ho_Chi_Minh');
    $weekday = date("l");
    $weekday = strtolower($weekday);
    switch($weekday) {
        case 'monday':
            $weekday = 'Thứ hai';
            break;
        case 'tuesday':
            $weekday = 'Thứ ba';
            break;
        case 'wednesday':
            $weekday = 'Thứ tư';
            break;
        case 'thursday':
            $weekday = 'Thứ năm';
            break;
        case 'friday':
            $weekday = 'Thứ sáu';
            break;
        case 'saturday':
            $weekday = 'Thứ bảy';
            break;
        default:
            $weekday = 'Chủ nhật';
            break;
    }
    return $weekday.', '.date('d/m/Y | H:i');
}

function trend_tags()
{
    // Save each unique tag and their count in this temp array.
    $tags_counts = array();
    
    $array_tags = array();
    //flag check whether is enough 12 tags
    $flag_check_enough_9 = false;
    
    $args = array(
       	'posts_per_page'      => 12,
        'meta_key'            => 'post_views_count',
        'orderby'             =>  'meta_value_num',
        'order'               => 'DESC', 
    );
    
    $the_query = new WP_Query($args);
    //var_dump($the_query);
    // The Loop
    if ( $the_query->have_posts() ) {
                                	
       	while ( $the_query->have_posts() ) {
      		$the_query->the_post();
            $post_tags = wp_get_post_tags( get_the_ID());
            
            if ( empty( $post_tags )) {
                continue; // Skip post if no tags set.
            }
            foreach ( $post_tags as $tag ) {
                if ( array_key_exists( $tag -> slug, $tags_counts )) {
                    // Increment tag count if tag already present.
                    $tags_counts[ $tag -> slug ] = $tags_counts[ $tag -> slug ] + 1;
                }
        
                else {
                    // New tag found, add to the counts list.
                    $tags_counts[ $tag -> slug ] = 1;
                    array_push($array_tags, $tag);
                    if(count($array_tags) == 9)
                        $flag_check_enough_9 = true;
                }
                if($flag_check_enough_9) // if enough 12 tag then break
                    break;
            }
            //array_push($array_tags, $tags_ob);
                    
            //the_title();
            if($flag_check_enough_9) // if enough 12 tag then break
                break;
        }
    }
    
    wp_reset_query();  // Restore global post data stomped by the_post().
    return $array_tags;
}
function get_random_color(){
    $colors = array('#4089EA', '#863b9d', '#BD4095', '#40BDB8','#e3646d', '#90c569');
    $i = rand(0, 5);
    return $colors[$i];
}

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}